(function($, document) {
	var CLASS_FORM = 'form';
	var CLASS_SUBMIT = 'submit';
	var CLASS_ERROR = 'error';
	var CLASS_SUBMIT_DISABLED = 'disabled';
	var submitBtnDisabled = true;
	var Validator = $.Validator = $.Class.extend({
		init: function(holder, options) {
			var self = this;
			if(!options) {
				throw "缺少验证参数";
			}
			self.holder = holder;
			self.rules = options.rules;
			self.success = options.success;
			self.validateError = [];
			for(i = 0; i < self.rules.length; i++) {
				self.validateError[i] = true;
			}
			self.validateSuccess;
			self.validateSuccessNums;
			self.submit = $.qsa('.' + CLASS_SUBMIT, self.holder)[0];
			self.validateLength = self.rules.length;
			self.initEvent();
		},
		initEvent: function() {
			var self = this;
			$.each(self.rules, function(i, rule) {
				document.getElementsByName(rule[0])[0].addEventListener('change', function(event) {
					var val = this.value;
					if(!self.validate(val, rule[1])) {
						this.classList.add('error');
						$.toast(rule[2]);
						self.validateError[i] = true;
					} else {
						self.validateError[i] = false;
					}
					if(submitBtnDisabled) {
						self.validateSuccess = true;
						for(o = 0; o < self.validateError.length; o++) {
							if(self.validateError[o] == true) {
								self.validateSuccess = false;
								break;
							}
						}
						if(self.validateSuccess) {
							self.submit.classList.remove(CLASS_SUBMIT_DISABLED);
						} else {
							self.submit.classList.add(CLASS_SUBMIT_DISABLED);

						}
					}

				});
				document.getElementsByName(rule[0])[0].addEventListener('focus', function(event) {

					if(this.classList.contains('error')) {
						this.classList.remove('error');
					}

				});

			});
			document.getElementsByClassName(CLASS_SUBMIT)[0].addEventListener('click', function(event) {
				var message = '';
				self.validateSuccessNums = 0;
				$.each(self.rules, function(i, rule) {
					var val = document.getElementsByName(rule[0])[0].value;
					if(!self.validate(val, rule[1])) {
						if(message == '') {
							message += rule[2];
						} else {
							message += '<br/>' + rule[2];
						}

						document.getElementsByName(rule[0])[0].classList.add('error');
						return;
					} else {
						self.validateSuccessNums++;
					}

				});
				if(message != '') {
					$.toast(message);
				}

				if(self.validateSuccessNums == self.validateLength) {
					self.success();
				}
				return false;

			});

		},
		validate: function(val, rule) {
			var rule = rule.split('|');
			switch(rule[0]) {
				case "mobile":
					var reg = /^(13\d|14[57]|15[^4,\D]|17[678]|18\d)\d{8}|170[059]\d{7}$/;
					return reg.test(val);
					break;
				case "digit":
					var reg = new RegExp("^[0-9]{" + rule[1] + "}$", "gim");
					return reg.test(val);
					break;
				case "idcode":
					var reg = /^\d{15}|\d{17}[0-9Xx]$/;
					return reg.test(val);
					break;
				case "email":
					var reg = /^\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}$/;
					return reg.test(val);
					break;
				case "chinese":
					var reg = new RegExp("^[\u4e00-\u9fa5]{" + rule[1] + "}$", "gim");
					return reg.test(val);
					break;
				case "required":
					val = val.replace(/(^\s*)|(\s*$)/g, "");
					return(val.length) == 0 ? false : true;
					break;
				default:
					return false;
					break;
			}
		}

	});

	$.fn.validator = function(options) {
		this.each(function(i, element) {
			if(element.validator) {
				return;
			}
			element.validator = new Validator(element, options);

		});
		return this[0] ? this[0].validator : null;
	}

})(mui, document);